package com.ruoyi.web.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil extends JsonSerializer<Date> {
	/**
	 * 获取当前日期
	 *
	 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate(String format) {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(currentTime);
		return dateString;
	}
    /**
	 * 获取当前日期-多少天时间
	 *
	 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDateOFF(String format,int day) {
		Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-day);
		Date currentTime =cal.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(currentTime);
		return dateString;
	}


	@SuppressWarnings("static-access")
	public static String getDate(Integer type) {
		// 获取今日日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//Date date = new Date();
		// 获取最近指定天数日期
		Calendar calendar = new GregorianCalendar();

		//calendar.setTime(date);

		calendar.add(calendar.DATE, type);

		String date2 = sdf.format(calendar.getTime());
		return date2;

	}

	   @SuppressWarnings("static-access")
		public static String getMonth(Integer type) {
			// 获取今日日期
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			//Date date = new Date();
			// 获取最近指定天数日期
			Calendar calendar = new GregorianCalendar();

			//calendar.setTime(date);

			calendar.add(calendar.MONTH, type);

			String date2 = sdf.format(calendar.getTime());
			return date2;
		}

	/**
	 * 日期格式化转String
	 * @param date
	 * @return
	 */
	public static String DateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
    }


	/**
	 * String 转date
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date StringToDateInF(String date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
    }

	/**
	 * String 转date
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date StringToDate(String date) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.parse(date);
    }






	@Override
	public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		 String formattedDate = sdf.format(value);
		 gen.writeString(formattedDate);

	}





	/**
	 * 时间戳转换成日期格式字符串
	 * @param seconds 精确到秒的字符串
	 * @param formatStr
	 * @return
	 */
	public static String timeStamp2Date(String seconds,String format) {

		if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
			return "";
		}
		if(format == null || format.isEmpty()){
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(Long.valueOf(seconds+"000")));
	}
	/**
	 * 日期格式字符串转换成时间戳
	 * @param date 字符串日期
	 * @param format 如：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String date2TimeStamp(String date_str,String format){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return String.valueOf(sdf.parse(date_str).getTime()/1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 取得当前时间戳（精确到秒）
	 * @return
	 */
	public static String timeStamp(){
		long time = System.currentTimeMillis();
		String t = String.valueOf(time/1000);
		return t;
	}
	/**
	 * 返回当前时间前几天的时间格式
	 * @param simpleDateFormat
	 * @param index
	 * @return
	 */
	public  String getOldDay(String simpleDateFormat,int index){
		SimpleDateFormat fmt = new SimpleDateFormat(simpleDateFormat);
		Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
		calendar.set(Calendar.DAY_OF_MONTH,index); //当前时间减去一天，即一天前的时间
		String date = fmt.format(calendar.getTime());
		return date;
	}
	public static void main(String[] args) {
		String timeStamp = timeStamp();
		System.out.println("timeStamp="+timeStamp); //运行输出:timeStamp=1470278082
		System.out.println(System.currentTimeMillis());//运行输出:1470278082980
		//该方法的作用是返回当前的计算机时间，时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数

		String date = timeStamp2Date(timeStamp, "yyyy-MM-dd HH:mm:ss");
		System.out.println("date="+date);//运行输出:date=2016-08-04 10:34:42

		String timeStamp2 = date2TimeStamp(date, "yyyy-MM-dd HH:mm:ss");
		System.out.println(timeStamp2);  //运行输出:1470278082
	}
}
